<template>
  <div class="error-page">
    <svg xmlns="http://www.w3.org/2000/svg" width="90" height="90" fill="#DBE1EC" viewBox="0 0 48 48">
      <path d="M22 30h4v4h-4zm0-16h4v12h-4zm1.99-10C12.94 4 4 12.95 4 24s8.94 20 19.99 20S44 35.05 44 24 35.04 4 23.99 4zM24 40c-8.84 0-16-7.16-16-16S15.16 8 24 8s16 7.16 16 16-7.16 16-16 16z" />
    </svg>
    <div class="title">{{ isChunkLoadingFailed?'正在加载...':message }}</div>
    <div class="title" v-show="statusCode===401">
      <button class="button button-positive" @click="signOut">重新登录</button>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'nuxt-error',
  props: ['error'],
  // Only on debug mode
  data () {
    return {
      isChunkLoadingFailed: false
    }
  },
  watch: {
    error (newErr) {
      if (newErr) {
        console.error(newErr)
      }
    }
  },
  computed: {
    statusCode () {
      return (this.error && this.error.statusCode) || 500
    },
    message () {
      return this.error.message || '无权限'
    }
  },
  methods: {
    ...mapActions({
      cleanCache: 'signOut'
    }),
    signOut () {
      this.cleanCache()
      location.replace('/login?type=login')
    }
  },
  mounted () {
    if (this.statusCode === 500 && /^Loading chunk (\d)+ failed\./.test(this.message)) {
      this.isChunkLoadingFailed = true
      setTimeout(() => {
        location.replace(location.href)
      }, 50000)
    }
  }
}
</script>

<style lang="scss" scoped>
.error-page {
  padding: 1rem;
  background: #f7f8fb;
  color: #47494e;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: sans-serif;
  padding: 150px;
  .chunk-error {
    font-size: $size16;
    padding: 10px;
    color: $dark06;
  }
  .title {
    font-size: $size16;
    padding: 10px;
    color: $dark06;
  }
}
</style>
